package com.victualler.core.config.permission.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.core.config.permission.dto.request.RolePermisisonSaveRequestDto;
import com.victualler.core.config.permission.entity.RolePermissionInfoDao;
import com.victualler.core.config.permission.repository.RolePermissionInfoQueryRepository;
import com.victualler.core.config.permission.repository.RolePermissionInfoRepository;
import com.victualler.core.config.permission.service.RolePermissionInfoService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class RolePermissionInfoServiceImpl extends BaseServiceImpl<RolePermissionInfoDao,RolePermissionInfoRepository,RolePermissionInfoQueryRepository> implements RolePermissionInfoService {

    /**
     * @author Guoshichang
     * @since 21:46 on 2018/9/9.
     * @param rolePermisisonSaveRequestDto
     * @Function  保存角色的权限列表
     */
    public void savePermissionsByRoleId(@RequestBody RolePermisisonSaveRequestDto rolePermisisonSaveRequestDto) {
        Long roleId = rolePermisisonSaveRequestDto.getRoleId();
        //删除角色原有权限列表
        List<RolePermissionInfoDao> oldList = this.baseRepository.findByRoleId(roleId);
        this.baseRepository.delete(oldList);
        //重新构建权限列表
        List<RolePermissionInfoDao> rolePermissionInfoDaoList = new ArrayList<>();
        for (Long permissionId : rolePermisisonSaveRequestDto.getPermissionIds()) {
            rolePermissionInfoDaoList.add(new RolePermissionInfoDao(roleId,permissionId));
        }
        this.baseRepository.save(rolePermissionInfoDaoList);
    }
}
